package com.teamwhole.modules.grid.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.teamwhole.common.model.ResultInfo;
import com.teamwhole.modules.grid.entity.Dckhtjb;
import com.teamwhole.modules.grid.entity.Grid;
import com.teamwhole.modules.grid.service.DckhtjbService;
import com.teamwhole.modules.grid.service.GridService;

@Component
@Path("/grid/dckhtjb")
@Consumes("application/json;charset=utf-8")
@Produces("application/json;charset=utf-8")
public class DckhtjbResource {

	@Autowired
	private DckhtjbService dckhtjbService;
	
	@Autowired
	private GridService gridService;
	
	@Path("get")
	@GET
	public Dckhtjb get(@QueryParam("id") String id) {
		return dckhtjbService.get(id);
	}
	//在列表中显示所有数据
	@Path("/list")
	@POST
	public List<Map<String , Dckhtjb>> findList(Dckhtjb model) {
		Grid grid = new Grid();
		List<Grid> list = gridService.findList(grid);
		List<Map<String , Dckhtjb>> list1 = new ArrayList<Map<String , Dckhtjb>>();
		for(Grid g : list){
			Map<String , Dckhtjb> map = new HashMap<String , Dckhtjb>();
			model.setGridId(g.getId());
			Dckhtjb entity = dckhtjbService.findByGridId(model);
			if(entity == null){
				entity= new Dckhtjb();
				entity.setGridId(g.getId());
			}
			map.put(g.getName(), entity);
			list1.add(map);
		}
		return list1;
	}
	
	//增加一条记录 
	@Path("/create")
	@POST
	public ResultInfo create(Dckhtjb model) {
		Dckhtjb entity = dckhtjbService.findByGridId(model);
		if(entity==null){
			dckhtjbService.save(model);
			return ResultInfo.ok();
		}else{
			return null;
		}
		
	}
	

	// 更新
	@Path("/update")
	@POST
	public ResultInfo update(Dckhtjb model) {
		dckhtjbService.save(model);
		return ResultInfo.ok();
	}

	// 删除
	@Path("/delete")
	@GET
	public ResultInfo delete(@QueryParam("id") String id) {
		dckhtjbService.delete(id);
		return ResultInfo.ok();
	}
}
